<?php
defined('IN_SITE') or exit('Access Denied');


/* debug by aming */
if($_GET['debug']){
	$data = $db->get_one("select catid from ".DB_PRE."content where contentid = $contentid");
	$catid=$data['catid'];
	$debugs = $db->get_one("select * from ".DB_PRE."category where catid = $catid");
	dump($debugs);
	unset($data);
}

switch($action)
{
    default:
		
		
		// 如果参数是自定义名称
		if(!$contentid && $prefix){
			$result = $db->get_one("select `contentid` from ".DB_PRE."content where prefix = '$prefix'");
			$contentid = $result['contentid'];
			unset($result);
		}
		
		// 主要获取该文章的栏目所在信息，例如详细页模板等等
		require_once MOD_ROOT.'content/content.class.php';
		$c = new content();
		$r = $c->get($contentid);
		$C = cache_read('category_'.$r['catid'].'.php');
		
		// 上一篇和下一篇
		$prev = $db->get_one("select * from ".DB_PRE."content where `contentid` < $contentid and `catid` = $C[catid] and status = 99 order by `listorder` DESC,`contentid` DESC");
		$next = $db->get_one("select * from ".DB_PRE."content where `contentid` > $contentid and `catid` = $C[catid] and status = 99 order by `listorder` DESC,`contentid` DESC");
	

		
		// 获取该文章的所有内容信息
		require_once CACHE_MODEL_PATH.'content_output.class.php';
		$out = new content_output();
		$data = $out->get($r);
		
		// debug
		//dump($data);
		extract($data);
	
		// 设置文章的头部信息、关键字等
		$title = strip_tags($title);
		$head['title'] = $title.'_'.$C['catname'].'_'.$SETTING['sitename'];
		$head['keywords'] = str_replace(' ', ',', $r['keywords']);
		$head['description'] = $r['description'];
		
		if(!$template) $template = $C['template_show'];
		if(!$C['defaultchargepoint'] && !$r['readpoint'])
		{
			header('Last-Modified: '.gmdate('D, d M Y H:i:s', $r['updatetime']).' GMT');
			header('Expires: '.gmdate('D, d M Y H:i:s', $r['updatetime']+CACHE_PAGE_CONTENT_TTL).' GMT');
			header('Cache-Control: max-age='.CACHE_PAGE_CONTENT_TTL.', must-revalidate');
		}
		
		include template($template);
		cache_page(CACHE_PAGE_CONTENT_TTL);
	break;
	
	case 'tag':
		$tag = $tag ? $tag : $name;
		$keyword = load('keyword.class.php','keyword');
		$data = $keyword->get($tag);
		if(!$data) showmessage("TAG: $tag 不存在！");
		$keyword->hits($tag);
		extract($data);
		
		// 所有tag标签连成的字符串，用于sql查询的时候where in()
		$data2 = $db->select("select `tag` from table_content_tag t");
		foreach($data2 as $k=>$v){
			$tags .= !$tags ? $v['tag'] : ','.$v['tag'];
		}
		
		$head['title'] = $tag.'-'.$SETTING['sitename'];
		
		include template('tag.html');
	break;
	
	// 点击率，调用方法
	//<script language="JavaScript" src="?module=content&action=count&contentid={$contentid}">...
	case 'count':
		if($contentid)
		{
			include 'content.class.php';
			$c = new content();
			$r = $c->get_count($contentid);
			if(!$r) exit;
			if ($SETTING['category_count'])
			{
				require_once 'count.class.php';
				$count = new count();
				$count->add($contentid);
			}
			extract($r);
			$c->hits($contentid);
			
		}
		elseif($specialid)
		{
			require_once MOD_ROOT.'comment/api/comment_api.class.php';
			$count = new comment_api();
			$comments = $count->get_count($specialid);
			$hits = 0;
		}
		echo "\$('#hits').html($hits)";
	break;
	
	case 'edit': 
		require_once MOD_ROOT.'content/content.class.php';
		$c = new content();
		$data = $c->get($contentid);
		extract($data);
		
		if(!isset($CATEGORY[$catid]) || $CATEGORY[$catid]['type'] != 0) showmessage('非法参数！');
		$category = cache_read("category_$catid.php");
		extract($category);
		

        require_once MOD_ROOT.'attachment/attachment.class.php';
		$attachment = new attachment($mod, $catid);

		if($dosubmit)
		{
			$contentid = intval($contentid);
			$r = $db->get_one("SELECT status FROM ".DB_PRE."content WHERE contentid=$contentid");
			if($r['status'] == 99 && $_userid <> 1) showmessage('您发布的信息已经通过审核，不能修改');
			$c->edit($contentid, $info);
			showmessage('修改成功！', $forward);
		}
		else
		{
			require_once('form.class.php');
			require CACHE_MODEL_PATH.'content_form.class.php';
			$content_form = new content_form($modelid);
			$forminfos = $content_form->get($data);
			$position = catpos($catid, SCRIPT_NAME.'?action=manage&catid={$catid}');
			$head['title'] = $CATEGORY[$catid]['catname'].'_修改信息_'.$PHPCMS['sitename'];
			include template('edit.html','content');
		}
		break;
	
	case 'contribute':
		
		// 关于PHP表单提交错误后返回内容消失问题
		/*是由于在表单提交页面中使用了 session_start 函数。该函数会强制当前页面不被缓存。解决办法为，在你的 Session_start 函数后加入 header("Cache-control: private"); 注意在本行之前你的PHP程序不能有任何输出。

还有基于session的解决方法，在session_start前加上
session_cache_limiter('nocache');// 清空表单
session_cache_limiter('private'); //不清空表单，只在session生效期间
session_cache_limiter('public'); //不清空表单，如同没使用session一般

但是直接设置session会使seesion出现cache，从而使应用到session的地方出现session信息不能更新的问题*/
		header("Cache-control: private");
		
		//require dirname(__FILE__).'/includes/common.inc.php';
		if(!$G['allowpost']) showmessage('你所在的用户组没有发表权限');
		
		
		require_once 'form.class.php';
		require_once MOD_ROOT.'content/content.class.php';
		
		$c = new content();
		
		$STATUS = cache_read('status.php');
		if(is_array($STATUS))
		{
			krsort($STATUS);
		}
		$head['title'] = '信息管理';
		
		if(!$contribute)
		{
			if(isset($CATEGORY[$catid]) && $CATEGORY[$catid]['type'] == 0)
			{
				$cats = submodelcat($CATEGORY[$catid]['modelid']);
				$categorys = '<select name="catid" id="catid" size="2" style="height:260px;width:350px;"><option value="0">请选择栏目</option>';
				foreach($cats AS $k=>$v)
				{
					$selected = '';
					if($v['child'] == 1) continue;
					if($v['child'] == 0 && $catid == $k) $selected = 'selected';
					$categorys .= "<option value='$k' $selected>$v[catname]</option>";
				}
				$categorys .= '</select>';
			}
			else
			{
				$categorys = form::select_category('content', 0, 'catid', 'catid', '请选择栏目', $catid, 'size="2" style="height:260px;width:350px;"', 1, 0);
			}
			include template('contribute.html',$module);
		}
		else
		{
			if($catid == '') showmessage('请选择栏目',$forward);
			if($CATEGORY[$catid]['child']) showmessage('请选择的栏目不允许发布信息，请重新选择',$forward);
			if(!isset($CATEGORY[$catid]) || $CATEGORY[$catid]['type'] != 0) showmessage('非法参数！');
			$category = cache_read("category_$catid.php");
			extract($category);
		
			require_once MOD_ROOT.'attachment/attachment.class.php';
			$attachment = new attachment($module, $catid);
		
			if($dosubmit)
			{
				checkcode($checkcodestr,1,'goback');
				$info['status'] = 99; // 默认发布审核
				$contentid = $c->add($info);
				showmessage('发布成功！', $forward);
			}
			else
			{
				require CACHE_MODEL_PATH.'content_form.class.php';
				$content_form = new content_form($modelid);
				$data['catid'] = $catid;
				$forminfos = $content_form->get($data);
				include template('contribute.html',$module);
			}
		}
	break;
}
?>